# -*- utf-8 -*-

import requests
import json
import re
from time import sleep
from bs4 import BeautifulSoup
import time
import datetime
from requests.adapters import HTTPAdapter
from time import strftime, localtime

def baojing(wenben):
    dingding = '钉钉群报警接口'

    headers = {
        'Content-Type': 'application/json'
    }
    aaa = {"msgtype": "text",
            "text": {
                "content": wenben
            }}
    resp = requests.post(url=dingding, headers=headers, json=aaa)
    print(resp)

def get_bs(link):
    headers2 = {
        'authority': 'www.zhihu.com',
        'method': 'GET',
        'path': '/api/v4/search_v3?t=general&q=%E7%9F%B3%E5%8A%9B%E6%B4%BE&correction=1&offset=0&limit=20&lc_idx=0&show_all_topics=0&time_zone=a_week',
        'scheme': 'https',
        'accept': '*/*',
        'accept-encoding': 'gzip, deflate',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cookie': '_zap=3a486c4a-1291-42c3-912c-f96d7a910180; d_c0="ANCdGqEilBGPTm8xWR1ints1iGllp2jcIi4=|1594778965"; _ga=GA1.2.417443670.1594778961; tst=r; q_c1=d85beb88b7d84276bee9795888f94182|1594779227000|1594779227000; _gid=GA1.2.2029331231.1595397710; _xsrf=28bcd185-8aaa-416a-bbcf-8a594bc99ed3; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1595473431,1595474332,1595556044,1595577078; SESSIONID=j5mIgyGGFFgfoeOwXXoWAhmWDPDAwCpgLO8wylKcKSe; JOID=W1sSBkLkXWEM2me9Qej3_ghySSNehGwOSb0U_gSiEQJouQL4MtX_dV_QabtPeeQtENn9td9VJN3rwf_rbOCpDSU=; osd=UVgXBkPuXmQM222-ROj29At3SSJUh2kOSLcX-wSjGwFtuQPyMdD_dFXTbLtOc-coENj3ttpVJdfoxP_qZuOsDSQ=; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1595581571; capsion_ticket="2|1:0|10:1595581584|14:capsion_ticket|44:ZTk1MjdiNTc0YmYxNGJlMmEwMDgzMTI4OGQyMmIwZGM=|2d8d6b8cb1d49bf9f96ef92fbb9897c8df09aaf406818948e2f7ad50f28e3760"; KLBRSID=fe0fceb358d671fa6cc33898c8c48b48|1595581789|1595581501; _gat_gtag_UA_149949619_1=1',
        'referer': 'https://www.zhihu.com/search?q=%E7%9F%B3%E5%8A%9B%E6%B4%BE&range=1w&type=content&utm_content=search_history',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36',
        'x-ab-param': 'li_ebook_gen_search=0;qap_question_visitor= 0;se_whitelist=0;ug_follow_topic_1=2;se_hi_trunc=0;zr_sim3=0;zr_test_aa1=0;se_recommend=0;se_v053=0;zr_intervene=0;zr_rel_search=base;zr_training_first=false;se_zp_boost=0;tp_topic_tab=0;tp_m_intro_re_topic=1;zr_search_sim2=2;se_v_v005=0;tp_discover=0;tp_topic_style=0;zr_topic_rpc=0;tp_club_top=0;tp_club__entrance2=1;tp_club_feed=0;tp_zrec=0;top_test_4_liguangyi=1;pf_creator_card=1;zw_sameq_sorce=999;se_club_ui=0;se_v046=0;top_universalebook=1;pf_adjust=0;li_vip_verti_search=0;zr_expslotpaid=1;tp_contents=2;se_adsrank=4;li_car_meta=0;se_searchwiki=0;tp_club_fdv4=0;tp_meta_card=0;tp_topic_tab_new=0-0-0;pf_foltopic_usernum=50;se_usercard=0;tsp_ios_cardredesign=0;ls_fmp4=0;li_yxxq_aut=A1;tp_fenqu_wei=0;pf_fuceng=1;se_entity22=0;tp_clubhyb=0;tsp_ad_cardredesign=0;se_col_boost=0;se_colorfultab=1;se_sug_term=0;ls_recommend_test=0;zr_km_answer=open_cvr;zr_search_paid=1;se_preset=0;soc_feed_intelligent=3;zr_rerank=0;se_bert128=0;pf_newguide_vertical=0;li_yxzl_new_style_a=1;qap_labeltype=1;ls_videoad=2;tp_sft=a;zr_slot_training=1;se_merge=0;se_v_rate=0;se_click_v_v=0;top_ebook=0;se_aa_base=0;se_guess=0;pf_noti_entry_num=0;ls_video_commercial=0;li_svip_tab_search=1;se_return_1=0;se_v_v006=0;tp_dingyue_video=0;tsp_adcard2=0;pf_profile2_tab=0;li_answer_card=0;se_major=0;se_auth_src=0;top_quality=0;li_topics_search=0;top_v_album=1;li_edu_page=old;li_panswer_topic=0;zr_rec_answer_cp=open;se_wil_act=0;se_searchvideo=3;se_vbert3=0;se_major_v2=0;zr_slotpaidexp=1;ge_ge01=5;se_videobox=0;tp_club_bt=0;ug_newtag=1;se_ffzx_jushen1=0;tp_club_qa_entrance=1;li_paid_answer_exp=0;qap_question_author=0;zr_ans_rec=gbrank;zr_art_rec=base;soc_notification=1;li_catalog_card=1;ge_ge02=6;se_v_drop=0;tsp_hotlist_ui=1;tsp_ioscard2=0;li_viptab_name=0;li_video_section=0;zr_training_boost=false;se_mobilecard=0;li_svip_cardshow=1;zr_search_topic=1;se_topicfeed=0;se_college=default;se_t2sug=0;tp_header_style=1;tp_club_entrance=1;top_root=0',
        'x-api-version': '3.0.91',
        'x-app-za': 'OS=Web',
        'x-requested-with': 'fetch',
        'x-zse-83': '3_2.0',
        'x-zse-86': '1.0_aTS0FU9yUBOpeLS8fRtqkHu0b8OxQHF0TgtqeQu0FCSx'
                }

    headers3 = {
        'authority': 'www.zhihu.com',
        'method': 'GET',
        'path': '/question/383630430',
        'scheme': 'https',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'accept-encoding': 'gzip, deflate',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'max-age=0',
        'cookie': 'SESSIONID=Vzdq55unu3uHi58XmDsKwvXBhB8jyBerGRTv3Cw81li; JOID=U1EUAEL3gIsi_MQuZPUgGipS5Lp13KuqC9fvBUXcq6AD1e8FT-7LmX_8xCRsMZZzXLrylcnjQg8V8ZgEfhEaom8=; osd=U1kcAUr3iIMj9MQmbPQoGiJa5bJ11KOrA9fnDUTUq6gL1OcFR-bKkX_0zCVkMZ57XbLyncHiSg8d-ZkMfhkSo2c=; _zap=3a486c4a-1291-42c3-912c-f96d7a910180; d_c0="ANCdGqEilBGPTm8xWR1ints1iGllp2jcIi4=|1594778965"; _ga=GA1.2.417443670.1594778961; tst=r; q_c1=d85beb88b7d84276bee9795888f94182|1594779227000|1594779227000; _gid=GA1.2.2029331231.1595397710; capsion_ticket="2|1:0|10:1595482997|14:capsion_ticket|44: YzkxMjEwMmExYTZmNDRiNzllNWRmNTBiYWRkZTZhZTk=|cdcb6a3c3dc1e4c71061f2418644719cc9f16f1015778b3f287f838048601e6d"; _xsrf=28bcd185-8aaa-416a-bbcf-8a594bc99ed3; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1595473225,1595473431,1595474332,1595556044; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1595556108; KLBRSID=975d56862ba86eb589d21e89c8d1e74e|1595556426|1595556055',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'none',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36'
    }

    f1 = open('已报警内容.txt', 'r+', encoding='utf-8')
    list1 = f1.readlines()  # readlines获得的直接就是列表了

    pp = requests.get(link, headers=headers2)
    ra = pp.text
    print(ra)
    if 'error' in ra:
        print('返回错误，知乎网页格式可能有变，或者参数传递需要更新....')
    if '石力派' not in ra:
        print('没有新的内容，30分钟后继续下一轮扫描')
    else:
        line = ra.split('highlight')
        for line1 in line:
            if 'description' in line1:
                excerpt = line1.split('excerpt":"')[1].split('"')[0]
                q_id = line1.split('question":{"id":"')[1].split('"')[0]
                # a_id = line1.split('object":{"id":"')[1].split('"')[0]
                title = line1.split('title":"')[1].split('"')[0]
                link = 'https://www.zhihu.com/question/' + q_id
                excerpt1 = re.sub('<(S*?)[^>]*>.*?|<.*? />', '', excerpt)
                # excerpt2 = re.sub(r'[\\/u003cem]', '', excerpt1) #这个是外面显示的回答概要

                title1 = re.sub('<(S*?)[^>]*>.*?|<.*? />', '', title)
                title2 = re.sub(r'[\\/u003cem]', '', title1)

                print('链接：' + link)
                print('知乎问题：' + title2)
                linkk = link + '\n'
                if linkk in list1: #如果后面运行会重复报警，要改成link+\n来判断
                    print('■■■■■■■■■■■■■链接已经报警过.................')
                else:
                    baojing('报警链接：' + link)
                    baojing('报警内容：' + title2)
                    f1.writelines(link + '\n')
                    list1.append(link)

                zw = requests.get(link, headers=headers3, timeout=150)
                ttt1 = zw.text
                if 'editableContent":"","excerpt":"' in ttt1:
                    ttt2 = ttt1.split('editableContent":"","excerpt":"')
                    for ttt3 in ttt2:
                        if 'collapsedBy' in ttt3:  # collapsed 含义未确认
                            ttt4 = ttt3.split('","collapsedBy')[0]
                            print('回答：' + ttt4)

            else:
                continue

# f = open('已报警内容.txt', 'w', encoding='utf-8')

while 1:

    # url = 'https://www.zhihu.com/api/v4/search_v3?t=general&q=%E7%9F%B3%E5%8A%9B%E6%B4%BE&correction=1&offset=0&limit=20&lc_idx=0&show_all_topics=0&time_zone=three_months'
    # 以石力派为关键字搜索知乎三个月的内容

    url = 'https://www.zhihu.com/api/v4/search_v3?t=general&q=%E7%9F%B3%E5%8A%9B%E6%B4%BE&correction=1&offset=0&limit=20&lc_idx=0&show_all_topics=0&time_zone=a_week'
    # 以石力派为关键字搜索知乎一周的内容

    try:

        page = get_bs(url)
        print('以石力派为关键词搜索知乎完毕...')

        print(strftime('%Y-%m-%d %H:%M:%S', localtime()))
        time.sleep(1800)
    except Exception as e:
        print(e)
        time.sleep(7200)
    continue





